package com.winsdom.controller.CustomerManagementController;

import com.winsdom.anno.Authentication;
import com.winsdom.annotation.OperationLog;
import com.winsdom.result.Result;
import com.winsdom.service.ICustomerBillingConfigService;
import com.winsdom.vo.CustomerBillingConfigQueryVO;
import com.winsdom.vo.CustomerBillingConfigUpdateVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import static com.winsdom.enums.AuthenticationType.CUSTOMER_MANAGEMENT;

/**
 * 开票配置控制器
 * 用于管理客户的开票配置信息，包括查询和更新客户的开票设置
 */
@Slf4j
@RestController
@RequestMapping("/invoice-config")
@Api(tags = "客户管理接口", description = "客户开票配置管理接口")
@Authentication(value=CUSTOMER_MANAGEMENT)
public class InvoiceConfigurationController {

    @Autowired
    private ICustomerBillingConfigService customerBillingConfigService;

    @GetMapping("/query")
    @ApiOperation(value = "查询客户开票配置", notes = "根据客户ID查询其开票配置信息")
    @OperationLog(description = "查询客户开票配置")
    public Result<CustomerBillingConfigQueryVO> getCustomerBillingConfig(@RequestParam Long customerId) {
        log.info("查询客户开票配置，客户ID：{}", customerId);
        CustomerBillingConfigQueryVO config = customerBillingConfigService.getBillingConfigByCustomerId(customerId);
        return Result.success(config);
    }

    @PostMapping("/update")
    @ApiOperation(value = "更新客户开票配置", notes = "更新指定客户的开票配置信息")
    @OperationLog(description = "更新客户开票配置")
    public Result<Void> updateCustomerBillingConfig(@RequestBody CustomerBillingConfigUpdateVO updateVO) {
        log.info("更新客户开票配置，请求数据：{}", updateVO);
        try {
            customerBillingConfigService.updateBillingConfig(updateVO);
            return Result.success();
        } catch (Exception e) {
            log.error("更新客户开票配置失败", e);
            return Result.error(500, e.getMessage());
        }
    }
}